package com.goodfather.textbook.youdao.pad.pac;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.goodfather.textbook.youdao.pad.bean.BookDetail;
import com.goodfather.textbook.youdao.pad.bean.Http;
import com.goodfather.textbook.youdao.pad.bean.ProductionDetail;
import com.goodfather.textbook.youdao.pad.network.ApiConstant;
import com.goodfather.textbook.youdao.pad.network.JsonCallBack;
import com.goodfather.textbook.youdao.pad.util.L;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: zhou
 * @CreateDate: 2021/11/1
 */
public class BookPresenter {
    private IBookView iBookView;
    private boolean islock = false;//vip 解锁避免重复发送接口

    public BookPresenter(IBookView iBookView) {
        this.iBookView = iBookView;
    }

    public void getBookDetail(String bookId, String publishingId) {
        Map<String, String> map = new HashMap<>();
        map.put("bookId", bookId);
        map.put("publishingId", publishingId);
        OkGo.<Http<BookDetail>>post( ApiConstant.BOOK_DETAIL_1_URL)
                .tag(this)
                .headers(ApiConstant.getHeaders(iBookView.getContext()))
                .params(map)
                .execute(new JsonCallBack<Http<BookDetail>>() {
                    @Override
                    public void onSuccess(Response<Http<BookDetail>> response) {
                        if (response.body() != null) {
                            L.d("body===" + JSONObject.toJSONString(response.body()));
                            iBookView.showBookDetail(response.body().getData());
                        }
                    }
                });
    }

    public void getProductionDetail(String publishingId, final boolean afterLogin) {
        Map<String, String> map = new HashMap<>();
        map.put("productionId", publishingId);
        OkGo.<Http<ProductionDetail>>post(ApiConstant.PRODUCTION_DETAIL_URL)
                .tag(this)
                .headers(ApiConstant.getHeaders(iBookView.getContext()))
                .params(map)
                .execute(new JsonCallBack<Http<ProductionDetail>>() {
                    @Override
                    public void onSuccess(Response<Http<ProductionDetail>> response) {
                        if (response.body() != null) {
                            L.d("body===" + JSONObject.toJSONString(response.body().getData()));
                            iBookView.showProduction(response.body().getData(), afterLogin);
                        }
                    }
                });

    }

}
